package com.topscomm.main;

import com.topscomm.pub.util.SpringUtil;
import com.topscomm.gpm.utils.GetContextUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;

@SpringBootApplication
@EnableTransactionManagement
// @EnableDiscoveryClient
@ComponentScan(basePackages = { "com.topscomm" }, excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {com.topscomm.cbo.controller.CboApprovalNodeInsController.class}),
        @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.topscomm.cbo.service.impl.CboApprovalNodeInsService")
})
public class GPMApp implements TransactionManagementConfigurer {
    @Resource(name = "transactionManager")
    private PlatformTransactionManager defaultTxManager;

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(GPMApp.class, args);
        SpringUtil springUtil = (SpringUtil) SpringUtil.getBean("springUtil");
        springUtil.setApplicationContext(ctx);
        GetContextUtil.setApplicationContext(ctx);
    }

    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return defaultTxManager;
    }

    @Bean(name = "multipartResolver")
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("UTF-8");
        //resolveLazily属性启用是为了推迟文件解析，以在在UploadAction中捕获文件大小异常
        resolver.setResolveLazily(true);
        resolver.setMaxInMemorySize(40960);
        //上传文件大小 50M 50*1024*1024
        resolver.setMaxUploadSize(50 * 1024 * 1024);
        return resolver;
    }
//	@Bean
//	public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
//		RabbitTemplate template = new RabbitTemplate(connectionFactory);
//		template.setMessageConverter(new Jackson2JsonMessageConverter());
//		return template;
//	}
//
//	@Bean
//	public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
//		SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//		factory.setConnectionFactory(connectionFactory);
//		factory.setMessageConverter(new Jackson2JsonMessageConverter());
//		factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
//
//		return factory;
//	}
}
